package top.oylan.manager.service.impl;

import org.springframework.stereotype.Service;
import top.oylan.manager.entity.Banner;
import top.oylan.manager.entity.example.BannerExample;
import top.oylan.manager.mapper.BannerMapper;
import top.oylan.manager.service.BannerService;
import top.oylan.manager.tools.Pager;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-04-21
 */
@Service
public class BannerServiceImpl implements BannerService {

    @Resource
    BannerMapper bannerMapper;

    @Override
    public List<Banner> selectBannersByPager(Pager<Banner> pager) {

        BannerExample example = new BannerExample();
        BannerExample.Criteria criteria = example.createCriteria();
        Banner condition = pager.getCondition();

        pager.setTotalRow((int) bannerMapper.countByExample(example));

        example.setRowSize(pager.getEveyPageRow());
        example.setOffset((pager.getCurrentPage()-1)*pager.getEveyPageRow());

        return bannerMapper.selectByExample(example);
    }

    @Override
    public Banner findById(Integer id) {
        return bannerMapper.findByPrimaryKey(id);
    }

    @Override
    public int saveBanner(Banner banner) {
        return bannerMapper.insertSelective(banner);
    }

    @Override
    public int updateById(Banner banner, Integer id) {
        BannerExample example = new BannerExample();
        BannerExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        return bannerMapper.updateByExampleSelective(banner,example);
    }

    @Override
    public int deleteById(Integer id) {
        BannerExample example = new BannerExample();
        BannerExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        return bannerMapper.deleteByExample(example);
    }

    @Override
    public ArrayList<Banner> selectBannerByPosition(int pos) {
        BannerExample bannerExample = new BannerExample();
        BannerExample.Criteria criteria = bannerExample.createCriteria();
        criteria.andPositionEqualTo((byte)pos);
        return bannerMapper.selectByExample(bannerExample);
    }
}
